package cn.itcast.controller;

import cn.itcast.domain.UserCount;
import net.sf.jasperreports.engine.*;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class JasperController07 {

    /**
     * 父子报表
     */
    @GetMapping("/testJasper7")
    public void createPdf(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //1.引入jasper文件
        Resource resource = new ClassPathResource("templates/main01.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());

        //2.创建JasperPrint,向jasper文件中填充数据
        try (ServletOutputStream os = response.getOutputStream()) {
            Map<String, Object> parameters = new HashMap<>(8);
            //参数  子报表的路径   子报表需要的数据
            Resource subResout = new ClassPathResource("templates/testCharts.jasper");
            //子报表需要的数据
            parameters.put("sublist", getUserCountList());
            // 子报表的路径
            parameters.put("subpath", subResout.getFile().getPath());

            JasperPrint print = JasperFillManager.fillReport(fis, parameters, new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print, os);
        } catch (JRException e) {
            e.printStackTrace();
        }
    }

    /**
     * charts图形报表(下载)
     */
    @GetMapping("/createPdfAndDownload7")
    public void createPdfAndDownload(HttpServletResponse response) {
        setHeader(response);
        try (InputStream inputStream = Thread.currentThread().getContextClassLoader()
                .getResourceAsStream("templates/main01.jasper");
             OutputStream ouputStream = response.getOutputStream()) {

            Map<String, Object> parameters = new HashMap<>(8);
            //参数  子报表的路径   子报表需要的数据
            Resource subResout = new ClassPathResource("templates/testCharts.jasper");
            //子报表需要的数据
            parameters.put("sublist", getUserCountList());
            // 子报表的路径
            parameters.put("subpath", subResout.getFile().getPath());

            JasperPrint print = JasperFillManager.fillReport(inputStream, parameters, new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print, ouputStream);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setHeader(HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/octet-stream");
        response.setHeader("content-disposition", "attachment;filename=" + "text.pdf");
    }

    public List<UserCount> getUserCountList() {
        List<UserCount> list = new ArrayList<>();

        UserCount uc1 = new UserCount("传智播客", 1000L);
        UserCount uc2 = new UserCount("黑马程序员", 1000L);
        UserCount uc3 = new UserCount("baidu", 1000L);
        list.add(uc1);
        list.add(uc2);
        list.add(uc3);
        return list;
    }


}
